//
//  TJCustomeNavgationBar.m
//  TingJian
//
//  Created by lina on 2017/6/26.
//  Copyright © 2017年 刘晨光. All rights reserved.
//

#import "TJCustomeNavgationBar.h"
#import <Masonry.h>
#import <UIKit/UIKit.h>


@interface TJCustomeNavgationBar()

@property (nonatomic, copy) NSString *navTitil;
@property (nonatomic, strong) NSString *rightItemTitle;
@property (nonatomic, strong) NSString *rightItemImage;
@property (nonatomic, strong) NSString *leftBtnImgStr;
@property (nonatomic, strong) UILabel *rightBtnLabel;
@property (nonatomic, strong) UIImageView *rightBtnImageView;
@property (nonatomic, strong) UIImageView *backGroundImageView;

@end

@implementation TJCustomeNavgationBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        self.userInteractionEnabled = YES;
        [self setupSubViews];
        [self setupSubViewsLayout];
    }
    return self;
}

- (void)setupSubViews {
    
    self.backGroundImageView.frame = self.bounds;
    [self addSubview:self.backGroundImageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.leftNavigatoinItem];
    [self addSubview:self.rightNavigatoinItem];
    [self addSubview:self.rightBtnLabel];
    [self addSubview:self.rightBtnImageView];
    [self addSubview:self.bottomLine];
}

- (void)setupSubViewsLayout {
    
    [self.leftNavigatoinItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(48, 48));
        make.bottom.mas_equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(70);
        make.centerY.mas_equalTo(self.leftNavigatoinItem);
        make.right.mas_equalTo(self.mas_right).offset(-70);
    }];
    
    [self.rightNavigatoinItem mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(self.leftNavigatoinItem);
        make.right.mas_equalTo(self.mas_right).offset(-5);
        make.width.mas_equalTo(70);
        make.centerY.mas_equalTo(self.leftNavigatoinItem);;
        
    }];
    
    [self.rightBtnLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.centerY.mas_equalTo(self.leftNavigatoinItem);
        
    }];
    
    [self.rightBtnImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.centerY.mas_equalTo(self.leftNavigatoinItem);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.right.mas_equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
}
- (void)setNavgationTitle:(NSString *)title
{
    _navTitil = title;
    _titleLabel.text = _navTitil;
}

- (void)setRightNavigatoinItemImg:(UIImage *)rightNavigatoinItemImg{
    
    [self.rightBtnImageView setImage:rightNavigatoinItemImg];
    
}

#pragma mark - Property
- (UIView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] init];
    }
    return _bottomLine;
}

- (UIButton *)leftNavigatoinItem
{
    if (!_leftNavigatoinItem) {
        _leftNavigatoinItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftNavigatoinItem setImage:[UIImage imageNamed:@"arrow_back"] forState:UIControlStateNormal];
    }
    return _leftNavigatoinItem;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.text = @"标题";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.userInteractionEnabled = YES;
    }
    return _titleLabel;
}

- (UIButton *)rightNavigatoinItem
{
    if (!_rightNavigatoinItem) {
        
        _rightNavigatoinItem = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightNavigatoinItem.backgroundColor = [UIColor clearColor];
        _rightNavigatoinItem.titleLabel.textAlignment = NSTextAlignmentRight;
       
        
    }
    return _rightNavigatoinItem;
}

//右边按钮是文字
- (UILabel *)rightBtnLabel{
    
    
    if (!_rightBtnLabel) {
        
        _rightBtnLabel = [UILabel new];
        _rightBtnLabel.backgroundColor = [UIColor clearColor];
        _rightBtnLabel.textAlignment = NSTextAlignmentRight;
        _rightBtnLabel.font = [UIFont systemFontOfSize:15];
        _rightBtnLabel.textColor = [UIColor whiteColor];
    }
    return _rightBtnLabel;
}

- (UIImageView *)rightBtnImageView{
    
    if (!_rightBtnImageView) {
        
        _rightBtnImageView = [[UIImageView alloc] init];
    }
    return _rightBtnImageView;
}
@end
